package twentytwenty;

import java.util.*;

/**
 * @Author: Zahi
 * @Description:
 * @Date Created in 2020-12-12 17:16
 * @Modified By:
 */
public class Intersect_350 {
    public static void main(String[] args) {
        int [] nums1 = new int[]{1,2,3,2,1};
        int [] nums2 = new int[]{2,2,1};
        System.out.println(Arrays.toString(intersect(nums1, nums2)));
    }

    public static int[] intersect(int[] nums1, int[] nums2) {
        List<Integer> list = new ArrayList<>();
        for (int k : nums1) {
            for (int j = 0; j < nums2.length; j++) {
                if (nums2[j] == Integer.MAX_VALUE || k == Integer.MAX_VALUE) {
                    continue;
                }
                if (k == nums2[j]) {
                    list.add(nums2[j]);
                    k = Integer.MAX_VALUE;
                    nums2[j] = Integer.MAX_VALUE;
                }
            }
        }
        int [] result = new int[list.size()];
        int i = 0;
        for (Integer integer : list) {
            result[i++] = integer;
        }
        return result;
    }
}
